import java.io.File;
import java.nio.file.Files;

public class FileUtil {

    public boolean fileCopy(File sourceFolder,File destFolder,String fileName){
        try{
            File sourceFile = new File(sourceFolder,fileName);
            File destFile = new File(destFolder,fileName);
            //destFile.createNewFile();好像不需要提前创建目标文件
            if(!sourceFile.exists()) {
                System.out.println("The source file is not exist");
                return false;
            }else if(destFile.exists()){
                System.out.println("There is already a file with the same name "+fileName);
                return false;
            }else{
                Files.copy(sourceFile.toPath(), destFile.toPath());
                if(sourceFile.isDirectory()){//是文件夹则需要递归复制其中文件
                    for(File file:sourceFile.listFiles()){
                        fileCopy(sourceFile,destFile,file.getName());
                    }
                }
                System.out.println("The file " + fileName + " size is " + destFile.length() + " bytes.");
                return true;
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
}
